﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CharacterInfo : MonoBehaviour
{
    public Animator animator;
    /// <summary>
    /// 是否被选中
    /// </summary>
    public bool isSwitch;

    Transform panel_skin;
    [Header("皮肤信息")]
    public SkinClass thisSkin;                  //拖拽赋值
    public Image image;
    public RuntimeAnimatorController anim;

    [Header("用户是否拥有该皮肤")]
    public GameObject haveGot;
    public GameObject notGet;
    public Text clipNumText;

    private void Awake()
    {
        panel_skin = GameObject.Find("Panel_Character(Clone)").transform;
        UpdateSkinShow();
    }

    /// <summary>
    /// 选中/取消时的，状态改变
    /// </summary>
    /// <param name="toggle1"></param>
    public void ChangeByToggle(Toggle toggle1)
    {
        if (toggle1.isOn)
        {
            //Debug.Log("knock" + "——" + gameObject.name);
            //Panel_Character transform = panelCharacter.GetComponent<Panel_Character>();
            animator.runtimeAnimatorController = anim;
            isSwitch = true;
            User.instance.skinOnSwitch = this;

            if (thisSkin.haveGot)                                   
            {
                //GameControl.instance.curSkin = thisSkin;                 //将当前选中的皮肤信息传给 GameControl
                PlayerGetSkinInfo();

            }
            //else if (thisSkin.moneyNeed > 0 && User.instance.Money >= thisSkin.moneyNeed)       //金币足够时，可将金币皮肤传至GameControl 但只有在点击开始游戏时才会将该皮肤解锁
            //{
            //    //GameControl.instance.curSkin = thisSkin;
            //    PlayerGetSkinInfo();
            //}
        }
        else
        {
            isSwitch = false;
            Debug.Log(gameObject.name + "——isSwitch == false");
        }
    }

    /// <summary>
    /// 看广告，得皮肤（解锁后传参）
    /// </summary>
    public void WatchForSkin()
    {
        if (thisSkin.haveGot || !isSwitch)
        {
            //Debug.Log(thisSkin.haveGot + "——" + isSwitch + "——" + gameObject.name);
            return;
        }
        //Debug.Log(isSwitch + "——" + gameObject.name);

        //碎片皮肤
        if (thisSkin.clipNeed > 0)
        {
            thisSkin.clipHave++;

            if (thisSkin.clipHave == thisSkin.clipNeed)
            {
                thisSkin.haveGot = true;
                User.instance.SkinsHadNum++;
                //GameControl.instance.curSkin = Instantiate(thisSkin);
                PlayerGetSkinInfo();
            }
        }

        //试用皮肤
        if (thisSkin.isTrySkin)                     
        {
            thisSkin.haveGot = true;
            User.instance.SkinsHadNum++;
            //GameControl.instance.curSkin = thisSkin;
            PlayerGetSkinInfo();
        }

        UpdateSkinShow();
    }

    /// <summary>
    /// 在金币皮肤界面 购买皮肤。
    /// </summary>
    public void PayForSkin()
    {
        if (thisSkin.haveGot || !isSwitch)
        {
            return;
        }

        if (User.instance.Money >= thisSkin.moneyNeed)          //金币皮肤
        {
            User.instance.ChangeMoney(-thisSkin.moneyNeed);
            thisSkin.haveGot = true;
            User.instance.SkinsHadNum++;
            //GameControl.instance.curSkin = thisSkin;
            PlayerGetSkinInfo();
        }

        UpdateSkinShow();
    }

    /// <summary>
    /// 根据皮肤信息显示在皮肤界面上
    /// </summary>
    public void UpdateSkinShow()
    {
        if (thisSkin.haveGot)
        {
            haveGot.SetActive(true);
            notGet.SetActive(false);
            haveGot.GetComponent<Toggle>().isOn = true;
        }
        else
        {
            if (thisSkin.clipNeed > 0)
            {
                clipNumText.text = thisSkin.clipHave + "/" + thisSkin.clipNeed;
            }
        }

        if (panel_skin)
        {
            panel_skin.GetComponent<Panel_Character>().UpdatePanelData();
        }
    }

    /// <summary>
    /// 向角色传递皮肤信息
    /// </summary>
    public void PlayerGetSkinInfo()
    {
        //GameControl.instance.curSkin.idleImage = thisSkin.idleImage;
        //GameControl.instance.curSkin.tiltImage = thisSkin.tiltImage;
        //GameControl.instance.curSkin.winImage = thisSkin.winImage;

        GameControl.instance.curSkin.skinName = thisSkin.skinName;

        GameControl.instance.curSkin.levelNeed = thisSkin.levelNeed;
        GameControl.instance.curSkin.moneyNeed = thisSkin.moneyNeed;
        GameControl.instance.curSkin.clipHave = thisSkin.clipHave;
        GameControl.instance.curSkin.clipNeed = thisSkin.clipNeed;
        GameControl.instance.curSkin.isTrySkin = thisSkin.isTrySkin;
        GameControl.instance.curSkin.haveGot = thisSkin.haveGot;


    }



}
